package com.demonzym.framework.image.imagedownload;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;

import com.demonzym.data.trans.ProtocolConst;
import com.demonzym.dishorder.activity.DishorderBaseActivity;
import com.demonzym.framework.application.MyApplication;
import com.demonzym.framework.image.ImageProcess;
import com.demonzym.framework.net.ImageDownloadListener;
import com.demonzym.framework.net.ImageDownloadListener.ImageCallBack;
import com.demonzym.framework.string.MyString;

public class ImageManager {
	private final String TAG = "ImageManager";

	private Bitmap mDefaultProfile = null;

	private final int CACHE_DEFAULT_SIZE = 1000;
	private Map<String, Bitmap> mCatch = new HashMap<String, Bitmap>();// WeakHashMap
	private Map<String, List<IBitmapCb>> mCbs = new HashMap<String, List<IBitmapCb>>();

	private List<String> mNotCacheList = new LinkedList<String>();

	private Context mContext;

	private static ImageManager sInstance;

	synchronized public static ImageManager getInstance(Context context) {
		if (sInstance == null)
			sInstance = new ImageManager(context);

		return sInstance;
	}

	private ImageManager(Context context) {
		mContext = context;
	}

	/**
	 * 
	 * @param id
	 * @param url
	 * @param width
	 * @param height
	 * @param cb
	 */
	public void getImage(String id, ImageCallBack callback) {
//		if (MyString.isStringEmpty(id)) {
//			onGetImage(callback, null, getDefaultImage(), null);
//			return;
//		}
		id += ".jpg";
		if (false == getImageFromCache(id, callback)) {
			getImageFromServer(id, callback);
		}
	}
	
	private void getImageFromServer(String id, ImageCallBack cb){
		DishorderBaseActivity.con.getImage(id, cb);
	}

	private boolean getImageFromCache(String id, ImageCallBack cb) {
		Bitmap bitmap = null;
		File file = null;
		String filePath = getImageFilePath_Cache(id);
		file = new File(filePath);
		if (file.exists()) {
			bitmap = ImageProcess.getBitmapFromFileLimitSize(filePath, 0);
		}

		if (bitmap != null) {
			onGetImage(cb, id, bitmap, filePath);
			return true;
		}
		return false;
	}
	
	public void saveImageToCache(String id, byte[] bitmap_byte){
		FileOutputStream fos = null;
		String filePath = getImageFilePath_Cache(id);
		try {
			fos = new FileOutputStream(filePath);
			fos.write(bitmap_byte);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public interface IBitmapCb {
		public void onGetImage(String missionId, Bitmap bitmap, String localPath);

		public void onGetImageError(String missionId, int errCode);

		public void onDownloading(int max, int curr);
	}

	private void onGetImage(ImageCallBack cb, String id, Bitmap bmp,
			String localPath) {
		if (cb == null)
			return;

		cb.getBitmap(bmp);
	}

	synchronized public static String getImageCachePath() {
		return MyApplication.getAppInstance().getCacheDir().getPath() + "/";
	}

	private String getImageFilePath_Cache(String id) {
		if (id == null)
			return null;

		return getImageCachePath() + getImageFileName(id);
	}

	private String getImageFileName(String id) {
//		if (id == null)
//			return null;
//
//		return "image_" + id.hashCode();
		return id;
	}
}
